angular.factory(ꞌHttpsTestꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 35
rs 8.8571
1
(function () {
2
    'use strict';
3
4
    /**
5
     * @ngdoc function
6
     * @name passmanApp.controller:MainCtrl
7
     * @description
8
     * # MainCtrl
9
     * Controller of the passmanApp
10
     */
11
    angular.module('passmanExtension').factory('HttpsTest', ['$http', '$q', function ($http, $q) {
12
        var tester = {};
13
        tester.test = function (url) {
14
            var deferred = $q.defer();
15
            if(url.match(/^https?:\/\//)){
16
                deferred.resolve(url);
17
                return deferred.promise;
18
            }
19
            // first test with https
20
            var protocol = 'https://';
21
22
            var req = {
23
                method: 'GET',
24
                url: protocol+url,
25
                timeout: 500
26
            };
27
28
            $http(req).then(function () {
29
                    // we have https
30
                    deferred.resolve(protocol + url);
31
                },
32
                function () {
33
                    protocol = 'http://';
34
                    // we don't have https
35
                    deferred.reject(protocol + url);
36
                });
37
            return deferred.promise;
38
        };
39
40
        tester.isHTTP = function (url) {
41
            return url.substr(0,5) === 'http:';
42
        };
43
44
        return tester;
45
    }]);
46
}());
47